<template>
  <div>
    <el-card style="width: 40%; margin: 10px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item style="text-align: center" label-width="0">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/files/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="form.avatar" :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="原密码">
          <el-input v-model="password" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="newpassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="newpassword2" show-password></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button type="primary" @click="update">保存</el-button>
        <el-button type="warning" @click="back">返回</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Settings",
  data() {
    return {
      form: {},
      password:"",
      newpassword:"",
      newpassword2:""
    }
  },
  created() {
    let str = sessionStorage.getItem("administrator") || "{}"
    this.form = JSON.parse(str)
    request.post("/login/test",this.form)
  },
  methods: {
    handleAvatarSuccess(res) {
      this.form.avatar = res.data
      this.$message.success("上传成功")
      // this.update()
    },
    back(){
      this.$router.back()
    },
    update() {
      if (!(this.newpassword === this.newpassword2)){
        this.$message({
          type:"error",
          message:"两次输入的密码不同"
        })
      }
      else{
        let data={}
        let newdata={}
        let all={}
        data["username"]=this.form.username
        data["password"]=this.password
        newdata["username"]=this.form.username
        newdata["password"]=this.newpassword
        all["administrator1"]=data
        all["administrator2"]=newdata
        request.post("/login/modify",all).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
            sessionStorage.setItem("user", JSON.stringify(this.form))
            // 触发Layout更新用户信息
            this.$emit("userInfo")
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
        })
      }

    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
